package com.java.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.java.DictFeignClient;
import com.java.model.hosp.Hospital;
import com.java.model.vo.HospitalQueryVo;
import com.java.repository.HostpitalRespository;
import com.java.service.HospitalService;
import com.java.service.HospitalSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {

    //操控mongodb的东西
    @Autowired
    private HostpitalRespository hostpitalRespository;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //需要将paramMap数据 转换成 Hospital 对象  导入fastjson依赖
        String mapString  = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //拿到医院信息的hoscode
        String hoscode = hospital.getHoscode();
        //然后拿到这个对象后 在mongodb中查这个对象是否存在  根据mongodb springdata 的查询规范 可以在数据库中查询到结果
        //意思就是说：get read  根据hoscode 查询 Hospiral 数据
        Hospital hospitalExist =  hostpitalRespository.getHospitalByHoscode(hoscode);

        if(hospitalExist != null){
            //存在就修改
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        }else{
            //不存在就添加
            hospital.setStatus(1);//1 表示上线 0 表示下线
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hostpitalRespository.save(hospital);
        }


    }

    @Override
    public Hospital getHospital(String hoscode) {
        Hospital hospital = hostpitalRespository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * 查询分页从mongodb中进行分页
     * @param pageNum
     * @param pageSize
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public org.springframework.data.domain.Page selectPageList(
            Integer pageNum, Integer pageSize,
            HospitalQueryVo hospitalQueryVo) {
        //1、创建 pageRequest
        PageRequest pageRequest = PageRequest.of(pageNum-1, pageSize);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);

        //数据放到hospital类中
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital, matcher);
        org.springframework.data.domain.Page<Hospital> pages = hostpitalRespository.findAll(example, pageRequest);
        //
        String StringHostype = dictFeignClient.getName("Hostype", "1");
        pages.getContent().stream().forEach(item->{
            //得到item里面的hostype
            setHospitalHostype(item);
        });
        return pages;
    }

    //查询list集合 遍历进行医院等级封装
    private Hospital setHospitalHostype(Hospital hospital){
        //通过dictFeignClient 从数据字典中查询关于Hostype 通过医院的hosty等级 查询等级名称
        String StringHostype = dictFeignClient.getName("Hostype", hospital.getHostype());
        //通过省级代码查询医院名称
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        String city = dictFeignClient.getName(hospital.getCityCode());
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostype",StringHostype);
        hospital.getParam().put("fullAdress",province+city+district);
        return hospital;
    }


    @Override
    public void statusChanged(String id, Integer status) {
        Hospital hospital = hostpitalRespository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hostpitalRespository.save(hospital);
    }


//    @Override
//    public Map<String, Object> getHospitalById(String id) {
//        Map<String, Object> map = new HashMap<>();
//        Hospital hospital = setHospitalHostype(hostpitalRespository.findById(id).get());
//        map.put("hospital",hospital);
//        map.put("bookingRule",hospital.getBookingRule());
//        hospital.setBookingRule(null);
//        return map;
//    }


    @Override
    public Map<String, Object> getHospitalById(String id) {
        Map<String, Object> map = new HashMap<>();
        Hospital hospital = setHospitalHostype(hostpitalRespository.findById(id).get());
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHosNameByHoscode(String hoscode) {
        Hospital hospital = hostpitalRespository.getHospitalByHoscode(hoscode);
        if(hospital!=null){
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据医院名称模糊查询
     * @param hosname
     * @return
     */
    @Override
    public List<Hospital> getHospitalByhosname(String hosname) {
        List<Hospital> list =  hostpitalRespository.findHospitalByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> getHospitalByhoscode(String hoscode) {
        Map<String, Object> map = new HashMap<>();
        Hospital hospital = setHospitalHostype(hostpitalRespository.findHospitalByHoscode(hoscode));
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

}
